package popcorn.ticket.serviceimpl;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import popcorn.ticket.dao.LogsMapper;
import popcorn.ticket.dao.TicketMapper;
import popcorn.ticket.entity.Logs;
import popcorn.ticket.entity.Ticket;
import popcorn.ticket.service.TicketService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.GetEndTimeTools;
import popcorn.ticket.tool.PageData;
import popcorn.ticket.tool.RandomCodeTools;
import popcorn.ticket.tool.TestEWM;

@Service
public class TicketServiceImpl implements TicketService {
	@Autowired
	private TicketMapper tm;
	@Autowired
	private LogsMapper lm;

	AjaxResponse ar = new AjaxResponse();
	
	Logger logger = Logger.getLogger(TicketServiceImpl.class);
	
	@Override
	public PageData<Ticket> selectAll(Integer ticketid, int page, int limit,String seldate,int selstatus) {
		// 调用插件
		PageHelper.startPage(page, limit);
		String startdate = null;
		String enddate = null;
		if (!seldate.equals("")) {
			startdate=seldate.substring(0, 10);
			//结束日期
			enddate =seldate.substring(13);
		}

		List<Ticket> list = tm.selectAll(ticketid, startdate, enddate, selstatus);

		PageInfo<Ticket> pageInfo = new PageInfo<Ticket>(list);
		PageData<Ticket> pageData = new PageData<Ticket>(pageInfo.getList(), page, (int) pageInfo.getTotal(), limit);
		pageData.setCode(0);
		pageData.setMsg("");
		return pageData;
	}

	/**
	 * 生成影票
	 */
	@Override
	public AjaxResponse insert(Ticket record) {
		// 生成随机影票编号
		int suiji = Integer.valueOf(RandomCodeTools.getRan());
		record.setTicketid(suiji);
		
		int n = tm.insert(record);
		
		TestEWM ewm = new TestEWM();
		String content = suiji + "";
		try {
			ewm.encode(content);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// 判断
		if (n > 0) {
			ar.setCode(200);
			ar.setMsg("生成成功");
			return ar;
		}
		ar.setCode(0);
		ar.setMsg("生成失败");
		return ar;
	}

	/**
	 * 修改订单状态
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public AjaxResponse updateDY(Ticket record) {
		try {
			int n = tm.updateByPrimaryKeySelective(record);
			//判断是否修改成功
			System.out.println(1/n);
			// 判断该操作是什么操作
			// 修改显示状态
			if (record.getTicketstatus() == null && record.getTicketcondition() == 1) {
				// 获取ip
				InetAddress ia = InetAddress.getLocalHost();
				// 日志
				Logs logs = new Logs();
				// ip地址
				logs.setLogip(ia + "");
				logs.setLogobject("ticket");
				logs.setLogop("修改影票");
				logs.setLogtime(new Date());
				logs.setRemark("管理员修改影票显示状态");
				logs.setUsers(record.getOrders().getUsers());
				int l = lm.insert(logs);
				//判断是否添加成功
				System.out.println(1/l);
			}
			ar.setCode(200);
			ar.setMsg("修改成功");
			return ar;
		} catch (Exception e) {
			// 打印轨迹
			e.printStackTrace();
			ar.setCode(0);
			ar.setMsg("修改失败");
			return ar;
		}
	}
	
	/**
	 * 根据用户编号获取影票信息
	 */
	@Override
	public List<Ticket> selectTicketByUserid(Integer userid) {
		List<Ticket> list = tm.selectTicketByUserid(userid);
		
		for (int i = 0; i < list.size(); i++) {
			// 根据orderid 获取 电影时长
			list.get(i).setEndtime(GetEndTimeTools.getEndDate(list.get(i).getPlaytime(), list.get(i).getDuration()));
		}
		
		return list;
	}
	
	/**
	 * 用户删除影票  -- 实际只是修改显示状态
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public AjaxResponse delTicketByUser(int ticketid, Logs log) {
		Ticket t = new Ticket();
		t.setTicketid(ticketid);
		t.setTicketcondition(0);
		try {
			logger.info("ticket"+t.getTicketid());
			int n = tm.updateByPrimaryKeySelective(t);
			int n1 = lm.insert(log);
			logger.info("n:"+n);
			logger.info("n1:"+n1);
			System.out.println(1/n);
			System.out.println(1/n1);
			ar = new AjaxResponse("删除成功", 200);
			return ar;
		} catch (Exception e) {
			ar = new AjaxResponse("删除失败", 0);
			return ar;
		}
	}

	/**
	 * 获取有相同订单号的影票
	 */
	@Override
	public List<Integer> judegeTicketNum(int userid) {
		List<Ticket> list = tm.selectTicketByUserid(userid);
		List<Integer> olist = new ArrayList<Integer>();
		// 判断是否有orderid相同的
		for (int i = list.size() - 1; i > 0; i--) {
			for (int j = 0; j < i; j++) {
				if (list.get(i).getOrders().getOrderid().equals(list.get(j).getOrders().getOrderid())) {
					olist.add(list.get(i).getOrders().getOrderid());
				}
			}
		}
		return olist;
	}
	
	@Override
	public List<Ticket> getTicketByOrderid(int orderid) {

		List<Ticket> list = tm.selTicketByOrderid(orderid);
		return list;
	}
	
	@Override
	public List<Ticket> selTicketByScheduleid(int scheduleid) {
		// TODO Auto-generated method stub
		return tm.selTicketByScheduleid(scheduleid);
	}
	
	@Override
	public int updTicketPlayStatus(Ticket ticket) {
		
		return tm.updateByPrimaryKeySelective(ticket);
	}
	
}
